<?php

use yii\db\Migration;

class m2025_01_20_create_status_detallado_table extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%status_detallado}}', [
            'id' => $this->primaryKey(),
            'cotizacion_id' => $this->integer()->notNull(),
            'invoice_id' => $this->integer(),
            'cotizacion_item_id' => $this->integer(),
            
            // Identificadores
            'numero_pi' => $this->string(100),
            'numero_oc' => $this->string(100),
            'numero_invoice' => $this->string(100),
            'imp' => $this->string(100),
            
            // Referencias
            'jefe_id' => $this->integer(),
            'linea_producto_id' => $this->integer(),
            'proveedor_id' => $this->integer(),
            
            // Producto
            'codigo_producto' => $this->string(100),
            'descripcion_producto' => $this->string(500),
            'descripcion_2' => $this->string(500),
            
            // Cantidades y precios
            'precio_fob_unitario' => $this->decimal(12, 4)->defaultValue(0),
            'inner_ctn' => $this->integer()->defaultValue(0),
            'total_ctn' => $this->integer()->defaultValue(0),
            'total_unidades' => $this->integer()->defaultValue(0),
            'total_usd_pi' => $this->decimal(14, 2)->defaultValue(0),
            
            // Facturación
            'total_facturado_invoice' => $this->decimal(14, 2)->defaultValue(0),
            'deposit_pi' => $this->decimal(14, 2)->defaultValue(0),
            'deposit_aplicado_invoice' => $this->decimal(14, 2)->defaultValue(0),
            'balance_invoice' => $this->decimal(14, 2)->defaultValue(0),
            'saldo_pendiente' => $this->decimal(14, 2)->defaultValue(0),
            'saldo_a_favor' => $this->decimal(14, 2)->defaultValue(0),
            
            // Fechas
            'fecha_pi' => $this->date(),
            'fecha_entrega_prometida' => $this->date(),
            'fecha_entrega_real' => $this->date(),
            'eta' => $this->date(),
            
            // Estado y tracking
            'estado_actual' => $this->string(50),
            'color_estado' => $this->string(20), // 'verde', 'naranja', 'rojo'
            'dias_diferencia_entrega' => $this->integer()->defaultValue(0),
            'tipo_diferencia' => $this->string(20), // 'adelantado', 'a_tiempo', 'atrasado'
            
            // Metadata
            'fecha_actualizacion' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
            'actualizado_por' => $this->integer(),
            
            'KEY idx_cotizacion' => ['cotizacion_id'],
            'KEY idx_invoice' => ['invoice_id'],
            'KEY idx_estado' => ['estado_actual'],
            'KEY idx_jefe' => ['jefe_id'],
            'KEY idx_linea' => ['linea_producto_id'],
        ]);
        
        // Foreign keys
        $this->addForeignKey(
            'fk_status_detallado_cotizacion',
            '{{%status_detallado}}',
            'cotizacion_id',
            '{{%cotizaciones}}',
            'id',
            'CASCADE',
            'CASCADE'
        );
        
        $this->addForeignKey(
            'fk_status_detallado_invoice',
            '{{%status_detallado}}',
            'invoice_id',
            '{{%invoices}}',
            'id',
            'CASCADE',
            'CASCADE'
        );
        
        $this->addForeignKey(
            'fk_status_detallado_item',
            '{{%status_detallado}}',
            'cotizacion_item_id',
            '{{%cotizacion_items}}',
            'id',
            'CASCADE',
            'CASCADE'
        );
    }

    public function safeDown()
    {
        $this->dropTable('{{%status_detallado}}');
    }
}